/*
 * 公共工具类
 */

import prompt from '@system.prompt'
import router from '@system.router'

function queryString(url, query) {
  let str = []
  for (let key in query) {
    str.push(key + '=' + query[key])
  }
  let paramStr = str.join('&')
  return paramStr ? `${url}?${paramStr}` : url
}

function getQueryParams(url) {
  const queryString = url.split('?')[1]
  const queryObj = {}
  if (queryString) {
    // 4. 分割每个参数对
    const params = queryString.split('&')
    params.forEach(param => {
      const [key, value] = param.split('=')
      queryObj[decodeURIComponent(key)] = decodeURIComponent(value)
    })
  }

  return queryObj
}

function showToast(message = '', duration = 0) {
  if (!message) return
  prompt.showToast({
    message: message,
    duration
  })
}
function turnback() {
  router.back()
}
function formatDate(date, symbols = '-') {
  date = new Date(date)
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  let d = date.getDate()
  d = d < 10 ? '0' + d : d
  const monthDay = symbols == 'zh' ? m + '月' + d + '日' : m + symbols + d
  let w = date.getDay()
  let h = date.getHours()
  h = h < 10 ? '0' + h : h
  let min = date.getMinutes()
  min = min < 10 ? '0' + min : min
  const hoursMinutes = h + ':' + min
  const week = '周' + ['日', '一', '二', '三', '四', '五', '六'][w]
  const yearMonthDay =
    symbols == 'zh'
      ? y + '年' + m + '月' + d + '日'
      : y + symbols + m + symbols + d

  return {
    hoursMinutes,
    week,
    monthDay,
    yearMonthDay
  }
}
function isNumber(value) {
  return /^[0-9]$/.test(value)
}
function getLastWeekDays() {
  const days = []
  const today = new Date()
  const dayOfWeek = today.getDay()

  // 找到上周日的日期（包括今天在内）
  const sunday = new Date(today)
  sunday.setDate(today.getDate() - dayOfWeek)

  // 从上周日开始，获取一周内的日期
  for (let i = 0; i < 7; i++) {
    const currentDate = new Date(sunday)
    currentDate.setDate(sunday.getDate() + i)
    const day = currentDate.getDate()
    days.push(day)
  }

  return days
}
const expendArray = [
  {name:'早午晚餐',key:"1"},
  {name:'烟酒茶',key:"2"},
  {name:'水果零食',key:"3"},
  {name:'公共交通',key:"4"},
  {name:'日常用品',key:"5"},
  {name:'水电煤气',key:"6"},
  {name:'房租',key:"7"},
  {name:'物业管理',key:"8"},
  {name:'打车租车',key:"9"},
  {name:'私家车费用',key:"10"},
  {name:'衣服裤子',key:"11"},
  {name:'鞋帽包包',key:"12"},
  {name:'维修保养',key:"13"},
  {name:'话费',key:"14"},
  {name:'旅游度假',key:"15"},
  {name:'医药费',key:"16"},
  {name:'化妆饰品',key:"17"},
  {name:'运动健身',key:"18"},
  {name:'休闲娱乐',key:"19"},
  {name:'宠物宝贝',key:"20"},
  {name:'孝敬长辈',key:"21"},
  {name:'送礼请客',key:"22"},
  {name:'投资亏损',key:"23"},
  {name:'其他支出',key:"24"}
]
const incomeArray =[
  {name:'工资收入',key:"51"},
  {name:'奖金收入',key:"52"},
  {name:'经营所得',key:"53"},
  {name:'利息收入',key:"54"},
  {name:'兼职收入',key:"55"},
  {name:'投资收入',key:"56"},
  {name:'礼金收入',key:"57"},
  {name:'意外来钱',key:"58"},
  {name:'中奖收入',key:"59"},
  {name:'加班收入',key:"60"},
  {name:'其他收入',key:"61"}
]
export default {
  showToast,
  queryString,
  getQueryParams,
  formatDate,
  turnback,
  isNumber,
  getLastWeekDays,
  expendArray,
  incomeArray
}
